package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     *添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
      //先判该购物车菜品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setDishId(shoppingCartDTO.getDishId());
        shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        shoppingCart.setCreateTime(LocalDateTime.now());
        ShoppingCart list = shoppingCartMapper.list(shoppingCart);
        if(list != null){
            //如果购物车菜品已经存存在
            list.setNumber(list.getNumber()+1);
            shoppingCartMapper.update(list);
        }else{
            //如果购物车菜品不存在
            if(shoppingCartDTO.getDishId() != null){
                //如果是菜品查询菜品信息
                DishVO dish = dishMapper.getByIdWithFlavor(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                System.out.println("dish:"+dish);
                System.out.println("image:"+dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setNumber(1);
                shoppingCartMapper.insert(shoppingCart);
            } else{
                //如果是套餐查询套餐信息
                SetmealVO byId = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setNumber(1);
                shoppingCartMapper.insert(shoppingCart);
            }

        }


    }
    /**
     * 获取当前用户的购物车数据
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.select(BaseContext.getCurrentId());
    }
    /**
     * 删除购物车数据
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //先查询被删除的是套餐还是菜品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        ShoppingCart byDishOrSetmealId = shoppingCartMapper.getByDishOrSetmealId(shoppingCart);
            if(byDishOrSetmealId.getNumber() >1){
                //如果菜品或套餐数量大于1则修改套餐
                byDishOrSetmealId.setNumber(byDishOrSetmealId.getNumber()-1);
                shoppingCartMapper.update(byDishOrSetmealId);
            }else {
                //如果套餐数据等于1则删除
                shoppingCartMapper.deleteById(byDishOrSetmealId.getId());
            }
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());

    }


}
